/**
 * <p>Title: AuthorityResourceImpl</p>
 * <p>Description: AuthorityResource接口的实现类
 * Authority Svc and UserGroupAuthority Svc</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author Chen Wei
 * @version 1.0
 * <pre>Histroy:
 *       2016-11-30  Chen Wei  Create
 *</pre>
*/
package cn.jhg.account.rest;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.jhg.account.entity.Authority;
import cn.jhg.account.entity.UserGroup;
import cn.jhg.account.service.AuthoritySvc;
import cn.jhg.account.util.JsonUtil;
import cn.jhg.account.util.ServiceResult;
@Component
public class AuthorityResourceImpl implements AuthorityResource {
	@Resource
	private AuthoritySvc authoritySvc; //注入权限管理类
	
	/**
	 * <p>新增权限</p>
	 * @param name 	权限名
	 * @param sdid 签名id
	 * @param description	权限描述
	 * @return 				权限auid 和 errorCode
	 */
	@Override
	public ServiceResult<Integer> addAuthority(int sdid,String name, String description) {
		Authority authority=new Authority();
		authority.setName(name);
		authority.setDescription(description);
		ServiceResult<Integer> result=authoritySvc.addAuthority(authority,sdid, 0);
		return result;
	}
	
	 /**
     * <p>更新权限 </p>
     * @param sdid 			签名id
     * @param id   			权限id
     * @param name			权限名
     * @param description	权限描述
     * @return				errorCode
     */
	@Override
	public ServiceResult<Integer> updateAuthority(int sdid,int id, String name, String description) {
		Authority authority=new Authority();
		authority.setId(id);
		authority.setName(name);
		authority.setDescription(description);
		ServiceResult<Integer> result=authoritySvc.updateAuthority(authority,sdid, 0);
		return result;
	}
	
	/**
     * <p>根据id删除权限</p>
     * @param id 权限id
     * @param 
     * @return errorCode
     */
	@Override
	public ServiceResult<Integer> deleteAuthority(int sdid,int id) {
		ServiceResult<Integer> result=authoritySvc.deleteAuthority(id,sdid, 0);
		return result;
	}
	
	/**
	 * <p>按权限 auid 查询权限</p>
	 * @param auid	权限ID
	 * @return		权限实体
	 */
	@Override
	public ServiceResult<Authority> queryAuthority(int auid) {
		ServiceResult<Authority> result=authoritySvc.getAuthorityById(auid, 0);
		return result;
	}
	
	/**
     * <p>给用户组添加权限</p>
     * @param ugid 		用户组ID
     * @param sdid 		签名id
     * @param authList	权限auid集合
     * @return			errorCode
     */
	@Override
	public ServiceResult<Integer> addUserGroupAuthority(int sdid,int ugid, List<Integer> auidList) {
		System.out.println(auidList);
		System.out.println(ugid);
		ServiceResult<Integer> result=authoritySvc.addUserGroupAuthority(ugid,sdid,auidList, 0);
		return result;
	}
	
	/**
     * <p>删除指定用户组权限</p>
     * @param ugid 		用户组ID
     * @param sdid 		签名id
     * @param authList	权限auid集合
     * @return			errorCode
     */
	@Override
	public ServiceResult<Integer> deleteUserGroupAuthority(int sdid,int ugid, List<Integer> auidList) {
		ServiceResult<Integer> result=authoritySvc.deleteUserGroupAuthority(ugid, auidList,sdid, 0);
		return result;
	}

	/**
	 *<p>按用户组 ugid 查询权限</p>
	 * @param ugid	用户组ID
	 * @return		权限实体
	 */
	@Override
	public ServiceResult<List<Authority>> queryUserGroupAuthority(int ugid) {
		ServiceResult<List<Authority>> usergroupAuthoritySet=authoritySvc.queryAuthoritys(ugid, 0);
		return usergroupAuthoritySet;
	}
	
	/**
     * <p>按用户组 ugid 查询不在指定用户组权限</p>
     * @param ugid
     * @return 权限实体的list集合
     */
	@Override
	public ServiceResult<List<Authority>> queryAuthorityExists(int ugid) {
		ServiceResult<List<Authority>> result = authoritySvc.queryAuthorityExists(ugid, 0);
		return result;
	}
	
	/**
	 * 通过权限名称获取权限
	 * @param name 权限名称
	 * @return ServiceResult<List<Authority>> 权限集合
	 */
	@Override
	public ServiceResult<List<Authority>> getAuthority(String name) {
		if(name==null){
			name="";
		}
		ServiceResult<List<Authority>> result=authoritySvc.findAuthorityByName(name, 1);
		return result;
	}

	/**
	 * 通过权限名称获取权限
	 * @param name 权限名称
	 * @return String 权限集合
	 */
	@Override
	public String queryAuthority(String name) {
		ServiceResult<Authority> result=authoritySvc.getAuthority(name);
		ServiceResult<String> res=new ServiceResult<String>();
		if(result.getData()==null){
			res.setData("true");
		}else{
			res.setData("false");
		}
		return JsonUtil.packaging(res);
	}
	/**
	 * 根据权限id获取有这个权限的用户组
	 * @param aid	权限id
	 * @return		data为用户组id 的List and ErrorCode
	 */
	@Override
	public ServiceResult<List<Integer>> findUserGroupByAid(int aid) {
		ServiceResult<List<Integer>> result=authoritySvc.findUserGroupByAid(aid);
		return result;
	}
	
	/**
	 * 根据权限id获取有这个权限的用户组
	 * @param aid	权限id
	 * @return		data为用户组 的List and ErrorCode
	 */
	@Override
	public ServiceResult<List<UserGroup>> findUserGroupsByAid(int aid) {
		ServiceResult<List<UserGroup>> result=authoritySvc.findUserGroupsByAid(aid);
		return result;
	}
	
	/**
	 * 查询权限名字或描述中出现关键子的记录
	 * @param str	 输入为空格分隔多关键字,多关键字间为“或”关系, 查询权限名字或描述中出现关键子的记录
	 * @return	权限ID的List and ErrorCode
	 */
	@Override
	public ServiceResult<List<Integer>> findAuthority(String str) {
		if(str==null){
			str="";
		}
		String newStr=str.replaceAll("[' ']+"," ");
		ServiceResult<List<Integer>> result=authoritySvc.findAuthority(newStr);
		return result;
	}
	
	/**
	 * 查询权限名字或描述中出现关键字的记录
	 * @param str	 输入为空格分隔多关键字,多关键字间为“或”关系, 查询权限名字或描述中出现关键字的记录
	 * @return	权限实体的List and ErrorCode
	 */
	@Override
	public ServiceResult<List<Authority>> findAuthorityDetail(String str) {
		if(str==null){
			str="";
		}
		String newStr=str.replaceAll("[' ']+"," ");
		ServiceResult<List<Authority>> result=authoritySvc.findAuthorityDetail(newStr);
		return result;
	}

	
	

	
	
}
